<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      body {
        position: relative;
      }
      p {
        font-size: 24px;
        font-weight: bold;
        margin-bottom: 10px;
      }
      #btn {
        /* display: none; */
        transform: translateX(70px);
        position: fixed;
        right: 0px;
        bottom: 40px;
        width: 70px;
        height: 70px;
        border-radius: 50%;
        background-color: pink;
        border: none;
        transition: all 0.7s;
      }
      nav {
        position: fixed;
        top: 0;
        width: 100%;
        height: 50px;
        text-align: center;
        line-height: 50px;
        background-color: skyblue;
        transform: translateY(-50px);
        transition: all 0.5s;
      }
    </style>
  </head>
  <body>
    <nav id="nav">
      头部导航栏
    </nav>
    <div class="box">
      <p>1</p>
      <p>2</p>
      <p>3</p>
      <p>4</p>
      <p>5</p>
      <p>6</p>
      <p>7</p>
      <p>8</p>
      <p>9</p>
      <p>10</p>
      <p>11</p>
      <p>12</p>
      <p>13</p>
      <p>14</p>
      <p>15</p>
      <p>16</p>
      <p>17</p>
      <p>18</p>
      <p>19</p>
      <p>20</p>
      <p>21</p>
      <p>22</p>
      <p>23</p>
      <p>24</p>
      <p>25</p>
      <p>26</p>
      <p>27</p>
    </div>
    <button id="btn">返回顶部</button>
  </body>
  <script>
    window.onscroll = function () {
      var scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      console.log(scrollTop);
      if (scrollTop > 350) {
        nav.style.transform = "translateY(0)";
        // btn.style.display = "block";
        btn.style.transform = "translateX(-40px)";
      } else {
        nav.style.transform = "translateY(-50px)";
        // btn.style.display = "none";
        btn.style.transform = "translateX(70px)";
      }
    };
    btn.onclick = function () {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    };
    //430px
  </script>
</html>
